
/*
 * 首页登录模块
 * */
define(function(require,exports,module){
	
	//创建form
	function createForm(){
		var inputWidth = 280,inputHeight = 30;
		var self = this;
		//登录表单
		return {
			eName:"formEx",
			url:'sys/login.do',
			alertFlag:false,
			onSave:function(retData){
				if(retData.result){
					self.jqDom.loadJs('js/boot/index');
				}else{
					$ui.alert(retData.info,function(){
						self.loginForm.find('.loginbox').textbox('setValue','');
						self.loginForm.find('.codeImg').click();
						self.loginForm.find('.loginbox:first').textbox('textbox').focus();
					});
				}
			},
			elements:[{
				eName:"div",
				cssClass:"login_input",
				elements:{
					eName:"textbox",
					cssClass:"loginbox",
					name:"userName",
					prompt:'请输入账号',
					invalidMessage:'账号必须输入4-11位',
					width:inputWidth,
					height:inputHeight,
					iconCls:'icon-man',
					validType:'length[4,11]',
					tipPosition:"left",
					required:true
				}
			},{
				eName:"div",
				cssClass:"login_input",
				elements:{
					eName:"passwordbox",
					cssClass:"loginbox",
					name:"password",
					prompt:'请输入密码',
					invalidMessage:'密码3-12位',
					validType:'length[3,12]',
					width:inputWidth,height:inputHeight,
					tipPosition:"left",
					required:true
				}
			},{
				eName:"div",
				cssClass:"login_input",
				elements:[{
					eName:"textbox",
					cssClass:"loginbox",
					id:"checkcode",
					name:"code",
					width:170,height:inputHeight,
					prompt:'请输入验证码',
					tipPosition:"left",
					validType:'length[4,4]',
					required:true
				},{
					eName:"img",
					src:'logincode.do?_' + new Date().getTime(),
					cssClass:"codeImg",
					width:100,height:inputHeight,
					onClick:function(){
						var t1 = new Date().getTime();
						$(this).attr('src',"logincode.do?t1=" + t1);
					}
				}]
			},{
				eName:"linkbutton",
				cssClass:"login_input",
				text:'登录',
				width:80,height:inputHeight,
				onClick:function(){
					//testUpload();
					self.loginForm.formEx('submit');
				}
			}]
		};
	}
	
	//入口函数
	function Main(jqDom,loadParams){
		var self = this;
		self.jqDom = jqDom;
		var formUI = createForm.call(self);
		var ui = {
			eName:"div",
			addMode:'html',//覆盖
			cssClass:"login_panel",
			elements:[{
			  	eName:"div",
			  	cssClass:"login_top",
			  	elements:{
			  		eName:'div',
			  		cssClass:"login_title",
			  		elements:{
			  			eName:'span',
			  			elements:'烟台市公安消防灭火救援移动指挥辅助决策平台'
			  		}
			  	}
			},{
				eName:'div',
				cssClass:'login_body',
				elements:formUI
			}]
		}
		jqDom.createUI(ui);
		self.loginForm = jqDom.findJq('formEx');
		bindEvent.call(self);
	}
	//绑定回车事件
	function bindEvent(){
		var self = this;
		var loginForm = self.loginForm;
		var boxObjs = null,
		textObjs = [],
		boxlen = 0;
		boxObjs = loginForm.find('.loginbox');
		boxlen = boxObjs.length;
		boxObjs.each(function(i,obj){//遍历所有textbox
			var obj = $(this).textbox('textbox');
			textObjs.push(obj);
			obj.keydown(function(e){
				var key = e.keyCode;
				if(key == 13){
					if($(boxObjs[i]).textbox('isValid')){
						if(i == boxlen - 1){
							$(boxObjs[i]).blur();
							loginForm.formEx('submit');
						}else{
							textObjs[i+1].focus();
						}
					}
				}
			});
		});
	}
	
	module.exports = Main;
	
});